Skip to content

Commit

Permalink
added permission check to other activities, bug fix, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Jul 20, 2024
1 parent d1b4a71 commit 681d365
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 32 deletions.
20 changes: 20 additions & 0 deletions app/src/main/java/org/nuclearfog/apollo/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

package org.nuclearfog.apollo;

import static android.Manifest.permission.POST_NOTIFICATIONS;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_MEDIA_AUDIO;
import static android.Manifest.permission.READ_MEDIA_IMAGES;

import android.os.Build;

/**
* App-wide constants.
*
Expand Down Expand Up @@ -58,6 +65,19 @@ public final class Config {

public static final float OPACITY_HIDDEN = 0.4f;

/**
* permissions used for Android 6+
*/
public static final String[] PERMISSIONS;

static {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
PERMISSIONS = new String[]{READ_MEDIA_AUDIO, READ_MEDIA_IMAGES, POST_NOTIFICATIONS};
} else {
PERMISSIONS = new String[]{READ_EXTERNAL_STORAGE};
}
}

/* This class is never initiated. */
private Config() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1548,10 +1548,8 @@ private void reloadQueue() {
if (pos >= 0 && pos < mPlayList.size()) {
mPlayPos = pos;
}
synchronized (this) {
clearCurrentTrackInformation();
openCurrentAndNext();
}
clearCurrentTrackInformation();
openCurrentAndNext();
if (mPlayer.initialized()) {
long seekpos = settings.getSeekPosition();
seekTo(seekpos >= 0 && seekpos <= mPlayer.getDuration() ? seekpos : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@

package org.nuclearfog.apollo.ui.activities;

import static android.Manifest.permission.POST_NOTIFICATIONS;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_MEDIA_AUDIO;
import static android.Manifest.permission.READ_MEDIA_IMAGES;
import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

Expand All @@ -24,7 +20,6 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -39,8 +34,10 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.SearchView.OnQueryTextListener;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import org.nuclearfog.apollo.Config;
import org.nuclearfog.apollo.R;
import org.nuclearfog.apollo.async.AsyncExecutor.AsyncCallback;
import org.nuclearfog.apollo.async.loader.SongLoader;
Expand Down Expand Up @@ -118,13 +115,6 @@ public abstract class ActivityBase extends AppCompatActivity implements ServiceB
protected final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentView());
// Control the media volume
setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Initialize the broadcast receiver
mPlaybackStatus = new PlaybackStatus(this);
songLoader = new SongLoader(this);

init(savedInstanceState);
// Play and pause button
mPlayPauseButton = findViewById(R.id.action_button_play);
// Shuffle button
Expand All @@ -145,14 +135,16 @@ protected final void onCreate(Bundle savedInstanceState) {
View nextButton = findViewById(R.id.action_button_next);
// background of bottom action bar
View bottomActionBar = findViewById(R.id.bottom_action_bar_background);
// Control the media volume
setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Initialize the broadcast receiver
mPlaybackStatus = new PlaybackStatus(this);
songLoader = new SongLoader(this);
// set bottom action bar color
bottomActionBar.setBackground(new HoloSelector(this));
// hide player controls
controls.setVisibility(View.INVISIBLE);

// Bind Apollo's service
MusicUtils.bindToService(this, this);

previousButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
bottomActionBar.setOnClickListener(this);
Expand All @@ -162,20 +154,16 @@ protected final void onCreate(Bundle savedInstanceState) {
mAlbumArt.setOnClickListener(this);

// check permissions before initialization
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] permissions;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permissions = new String[]{READ_MEDIA_AUDIO, READ_MEDIA_IMAGES, POST_NOTIFICATIONS};
} else {
permissions = new String[]{READ_EXTERNAL_STORAGE};
}
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PERMISSION_GRANTED) {
requestPermissions(permissions, REQ_CHECK_PERM);
return;
}
for (String permission : Config.PERMISSIONS) {
if (ContextCompat.checkSelfPermission(this, permission) != PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, Config.PERMISSIONS, REQ_CHECK_PERM);
return;
}
}
// initialize sub-class
init(savedInstanceState);
// Bind Apollo's service
MusicUtils.bindToService(this, this);
}

/**
Expand Down Expand Up @@ -261,8 +249,12 @@ public final void onRequestPermissionsResult(int requestCode, @NonNull String[]
return;
}
}
// show battery optimization dialog
ApolloUtils.openBatteryOptimizationDialog(this);
// initialize subclass
init(getIntent().getExtras());
// Bind Apollo's service
MusicUtils.bindToService(this, this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.PorterDuff;
import android.media.AudioManager;
import android.net.Uri;
Expand Down Expand Up @@ -48,6 +49,7 @@
import androidx.viewpager.widget.ViewPager;

import org.nuclearfog.apollo.BuildConfig;
import org.nuclearfog.apollo.Config;
import org.nuclearfog.apollo.R;
import org.nuclearfog.apollo.async.AsyncExecutor.AsyncCallback;
import org.nuclearfog.apollo.async.loader.AlbumSongLoader;
Expand Down Expand Up @@ -248,6 +250,13 @@ protected void onCreate(Bundle savedInstanceState) {
mProgress.getProgressDrawable().setColorFilter(themeColor, PorterDuff.Mode.SRC_IN);
mProgress.getThumb().setColorFilter(themeColor, PorterDuff.Mode.SRC_IN);
controls.setVisibility(View.INVISIBLE);
// go to home activity if there is any missing permission
for (String permission : Config.PERMISSIONS) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
NavUtils.goHome(this);
return;
}
}
// Bind Apollo's service
MusicUtils.bindToService(this, this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ else if (selection instanceof Song)
if (selection instanceof Album)
NavUtils.openArtistProfile(this, ((Album) selection).getArtist());
else if (selection instanceof Artist)
NavUtils.openArtistProfile(this, ((Artist) selection).getName());
NavUtils.openArtistProfile(this, selection.getName());
else if (selection instanceof Song)
NavUtils.openArtistProfile(this, ((Song) selection).getArtist());
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

package org.nuclearfog.apollo.ui.activities;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import org.nuclearfog.apollo.Config;
import org.nuclearfog.apollo.async.AsyncExecutor.AsyncCallback;
Expand All @@ -31,6 +34,7 @@
import org.nuclearfog.apollo.async.loader.SearchLoader;
import org.nuclearfog.apollo.model.Song;
import org.nuclearfog.apollo.utils.MusicUtils;
import org.nuclearfog.apollo.utils.NavUtils;
import org.nuclearfog.apollo.utils.ServiceBinder.ServiceBinderCallback;
import org.nuclearfog.apollo.utils.StringUtils;

Expand Down Expand Up @@ -87,6 +91,13 @@ protected void onCreate(Bundle savedInstanceState) {
mLoader = new SearchLoader(this);
shouldOpenAudioPlayer = mIntent.getBooleanExtra(OPEN_AUDIO_PLAYER, true);
mVoiceQuery = StringUtils.capitalize(mIntent.getStringExtra(SearchManager.QUERY));
// go to home activity if there is any missing permission
for (String permission : Config.PERMISSIONS) {
if (ContextCompat.checkSelfPermission(this, permission) != PERMISSION_GRANTED) {
NavUtils.goHome(this);
return;
}
}
MusicUtils.bindToService(this, this);
}

Expand Down

0 comments on commit 681d365

Please sign in to comment.