diff --git a/app/src/main/java/com/simplemobiletools/musicplayer/MainActivity.java b/app/src/main/java/com/simplemobiletools/musicplayer/MainActivity.java index 953af886..15b5e118 100644 --- a/app/src/main/java/com/simplemobiletools/musicplayer/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/musicplayer/MainActivity.java @@ -1,13 +1,18 @@ package com.simplemobiletools.musicplayer; +import android.Manifest; import android.content.Intent; +import android.content.pm.PackageManager; import android.os.Bundle; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; +import android.widget.Toast; import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; @@ -19,6 +24,7 @@ import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { + private final int STORAGE_PERMISSION = 1; private Bus bus; @Bind(R.id.playPauseBtn) ImageView playPauseBtn; @@ -33,6 +39,28 @@ public class MainActivity extends AppCompatActivity { ButterKnife.bind(this); bus = BusProvider.getInstance(); bus.register(this); + + if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + initializePlayer(); + } else { + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + if (requestCode == STORAGE_PERMISSION) { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + initializePlayer(); + } else { + Toast.makeText(this, getResources().getString(R.string.no_permissions), Toast.LENGTH_SHORT).show(); + } + } + } + + private void initializePlayer() { Utils.sendIntent(this, Constants.INIT); } diff --git a/app/src/main/java/com/simplemobiletools/musicplayer/MusicService.java b/app/src/main/java/com/simplemobiletools/musicplayer/MusicService.java index cd540a9b..e35832be 100644 --- a/app/src/main/java/com/simplemobiletools/musicplayer/MusicService.java +++ b/app/src/main/java/com/simplemobiletools/musicplayer/MusicService.java @@ -1,5 +1,6 @@ package com.simplemobiletools.musicplayer; +import android.Manifest; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; @@ -7,6 +8,7 @@ import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.pm.PackageManager; import android.content.res.Resources; import android.database.Cursor; import android.graphics.Bitmap; @@ -18,11 +20,13 @@ import android.os.Build; import android.os.IBinder; import android.os.PowerManager; import android.provider.MediaStore; +import android.support.v4.content.ContextCompat; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.view.View; import android.widget.RemoteViews; +import android.widget.Toast; import com.squareup.otto.Bus; @@ -62,17 +66,25 @@ public class MusicService extends Service bus.register(this); } - getSortedSongs(); - headsetPlugReceiver = new HeadsetPlugReceiver(); - incomingCallReceiver = new IncomingCallReceiver(this); - wasPlayingAtCall = false; - initMediaPlayerIfNeeded(); - createNotificationButtons(); - setupNotification(); + if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + getSortedSongs(); + headsetPlugReceiver = new HeadsetPlugReceiver(); + incomingCallReceiver = new IncomingCallReceiver(this); + wasPlayingAtCall = false; + initMediaPlayerIfNeeded(); + createNotificationButtons(); + setupNotification(); + } else { + Toast.makeText(this, getResources().getString(R.string.no_permissions), Toast.LENGTH_SHORT).show(); + } } @Override public int onStartCommand(Intent intent, int flags, int startId) { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + return START_NOT_STICKY; + } + String action = intent.getAction(); if (action != null) { switch (action) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f0262ffb..b0915255 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,5 @@ Simple Music Player Your playlist is empty + Permission for accessing the songs has not been granted