Skip to content

Commit

Permalink
v1.0.1 Fixed Crash on Android 11
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravjot committed Jul 19, 2021
1 parent ace7f74 commit 379668c
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 122 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

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

30 changes: 30 additions & 0 deletions .idea/jarRepositories.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

23 changes: 12 additions & 11 deletions app/build.gradle
@@ -1,14 +1,14 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 31

defaultConfig {
applicationId "com.droidheat.musicplayer"
minSdkVersion 21
targetSdkVersion 28
versionCode 33
versionName "0.9.6"
targetSdkVersion 31
versionCode 34
versionName "1.0.1"
multiDexEnabled true
}
buildTypes {
Expand All @@ -27,16 +27,17 @@ repositories {
}

dependencies {
def appcompat_version = "28.0.0"

implementation fileTree(include: ['*.jar'], dir: 'libs')

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:support-media-compat:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:mediarouter-v7:28.0.0'
implementation "com.android.support:appcompat-v7:$appcompat_version"
implementation "com.android.support:design:$appcompat_version"
implementation "com.android.support:support-v4:$appcompat_version"
implementation "com.android.support:cardview-v7:$appcompat_version"
implementation "com.android.support:support-media-compat:$appcompat_version"
implementation "com.android.support:recyclerview-v7:$appcompat_version"
implementation "com.android.support:mediarouter-v7:$appcompat_version"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.jenzz:materialpreference:1.3'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -3,13 +3,15 @@
package="com.droidheat.musicplayer">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:name=".NoadPlayer"
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:fullBackupContent="true"
android:hardwareAccelerated="true"
Expand Down
Expand Up @@ -324,6 +324,9 @@ private void doPushPlay(int id) {
}

private void processNextRequest() {
if (songsUtils.getCurrentMusicID() + 1 == songsUtils.queue().size()) {
return;
}
resetMediaPlayerPosition();

int musicID = songsUtils.getCurrentMusicID();
Expand Down Expand Up @@ -798,7 +801,9 @@ private void setMediaPlayer(String path) {

try {
getCurrentMediaPlayer().prepare();
} catch (IOException ignored) {
} catch (Exception e) {
e.printStackTrace();
(new CommonUtils(this)).showTheToast("Unable to play music file :/");
}
} else {
processNextRequest();
Expand Down
Expand Up @@ -11,6 +11,7 @@
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.PermissionChecker;
Expand Down Expand Up @@ -65,18 +66,22 @@ protected void onCreate(Bundle savedInstanceState) {

if (Build.VERSION.SDK_INT > 22) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED) {
Manifest.permission.READ_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_MEDIA_LOCATION) != PermissionChecker.PERMISSION_GRANTED) {
// No explanation needed, we can request the permission.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Request for permissions");
alertDialog.setMessage("For music player to work we need your permission to access" +
" files on your device.");
alertDialog.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_MEDIA_LOCATION},
1);
}
});
Expand Down
109 changes: 49 additions & 60 deletions app/src/main/java/com/droidheat/musicplayer/utils/SongsUtils.java
Expand Up @@ -55,14 +55,14 @@

public class SongsUtils {

private String TAG = "SongsManagerConsole";
private final String TAG = "SongsManagerConsole";

private Context context;
private SharedPrefsUtils sharedPrefsUtils;
private static ArrayList<SongModel> mainList = new ArrayList<>();
private static ArrayList<SongModel> queue = new ArrayList<>();
private static ArrayList<HashMap<String, String>> albums = new ArrayList<>();
private static ArrayList<HashMap<String, String>> artists = new ArrayList<>();
private final Context context;
private final SharedPrefsUtils sharedPrefsUtils;
private static final ArrayList<SongModel> mainList = new ArrayList<>();
private static final ArrayList<SongModel> queue = new ArrayList<>();
private static final ArrayList<HashMap<String, String>> albums = new ArrayList<>();
private static final ArrayList<HashMap<String, String>> artists = new ArrayList<>();

public SongsUtils(Context context) {
this.context = context;
Expand Down Expand Up @@ -617,67 +617,56 @@ private void grabIfEmpty() {
}

private void grabData() {
String[] STAR = {"*"};

boolean excludeShortSounds = sharedPrefsUtils.readSharedPrefsBoolean("excludeShortSounds", false);
boolean excludeWhatsApp = sharedPrefsUtils.readSharedPrefsBoolean("excludeWhatsAppSounds", false);

Cursor cursor;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = context.getContentResolver().query(uri, STAR, selection, null, null);

if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String duration = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DURATION));
int currentDuration = Math.round(Integer
.parseInt(duration));
if (currentDuration > ((excludeShortSounds) ? 60000 : 0)) {
if (!excludeWhatsApp || !cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM)).equals("WhatsApp Audio")) {
String songName = cursor
.getString(
cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME))
.replace("_", " ").trim().replaceAll(" +", " ");
String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
String title = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.TITLE)).replace("_", " ").trim().replaceAll(" +", " ");
String artistName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String albumName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));

String albumID = cursor
.getString(
cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)
);

TimeZone tz = TimeZone.getTimeZone("UTC");
SimpleDateFormat df = new SimpleDateFormat("mm:ss", Locale.getDefault());
df.setTimeZone(tz);
String time = String.valueOf(df.format(currentDuration));

// Adding song to list
SongModel songModel = new SongModel();
songModel.setFileName(songName);
songModel.setTitle(title);
songModel.setArtist(artistName);
songModel.setAlbum(albumName);
songModel.setAlbumID(albumID);
songModel.setPath(path);
songModel.setDuration(time);

mainList.add(songModel);
}
String[] projection = new String[] {
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ALBUM_ID
};

try (Cursor cursor = context.getContentResolver().query(
uri, projection, null, null, null
)) {
int songNameColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
int durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
int pathColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
int titleColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int artistColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
int albumColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
int albumIDColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);

while (cursor.moveToNext()) {
int currentDuration = Math.round((cursor.getInt(durationColumn)));
if (currentDuration > ((excludeShortSounds) ? 60000 : 0)) {
if (!excludeWhatsApp || !cursor.getString(albumColumn).equals("WhatsApp Audio")) {
TimeZone tz = TimeZone.getTimeZone("UTC");
SimpleDateFormat df = new SimpleDateFormat("mm:ss", Locale.getDefault());
df.setTimeZone(tz);
String time = String.valueOf(df.format(currentDuration));

// Adding song to list
SongModel songModel = new SongModel();
songModel.setFileName(cursor.getString(songNameColumn));
songModel.setTitle(cursor.getString(titleColumn));
songModel.setArtist(cursor.getString(artistColumn));
songModel.setAlbum(cursor.getString(albumColumn));
songModel.setAlbumID(cursor.getString(albumIDColumn));
songModel.setPath(cursor.getString(pathColumn));
songModel.setDuration(time);

mainList.add(songModel);
}
}
while (cursor.moveToNext());
}
cursor.close();
}

/*
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/progress_bar_music_dock.xml
Expand Up @@ -4,15 +4,15 @@
<item android:id="@android:id/background"
android:gravity="center_vertical">
<shape android:shape="rectangle">
<size android:height="4dp"/>
<size android:height="5dp"/>
<solid android:color="#44ffffff" />
</shape>
</item>
<item android:id="@android:id/progress"
android:gravity="center_vertical">
<clip>
<shape android:shape="rectangle">
<size android:height="4dp"/>
<size android:height="5dp"/>
<solid android:color="@color/accentColor" />
</shape>
</clip>
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/drawable/thumb_white.xml
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>

<gradient
android:startColor="@color/white"
android:centerColor="@color/white"
android:endColor="@color/white" />

<size
android:height="20dp"
android:width="20dp" />


</shape>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/widget_dark_gradient.xml
@@ -1,6 +1,6 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:startColor="#aa000000"
android:startColor="#ee000000"
android:endColor="#00000000"
android:centerX="0.65"
android:angle="90"/>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout-sw600dp/fragment_music_dock.xml
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_height="62dp"
android:background="@color/primaryDark">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_height="62dp"
android:background="?android:attr/selectableItemBackground">

<ImageView
Expand Down

0 comments on commit 379668c

Please sign in to comment.