check for storage permissions at start

This commit is contained in:
tibbi 2016-03-16 13:44:53 +01:00
parent 683b3a282c
commit 445e0272eb
3 changed files with 48 additions and 7 deletions

View file

@ -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);
}

View file

@ -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) {

View file

@ -1,4 +1,5 @@
<resources>
<string name="app_name">Simple Music Player</string>
<string name="playlist_empty">Your playlist is empty</string>
<string name="no_permissions">Permission for accessing the songs has not been granted</string>
</resources>