Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a896ae2f authored by Alexander Martinz's avatar Alexander Martinz Committed by Bruno Martins
Browse files

Eleven: prevent NPEs when requesting permissions



If Eleven is started for the first time and does not have
all permissions pregranted, it tries to update ui elements such
as playback controls, which do not exist yet and crashes.

When we request permissions, we need to skip automated binding to the
service to ensure everything is set up properly and call it manually
once we proceed the flow.

Test: Open Eleven in Android Studio and run it for the first time.

Change-Id: I8dc0c147a4ce89400098392367d06571fa3ada29
Signed-off-by: Alexander Martinz's avatarAlexander Martinz <amartinz@shiftphones.com>
parent 56ae82a7
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -110,6 +110,8 @@ public abstract class BaseActivity extends AppCompatActivity implements ServiceC

    private Drawable mActionBarBackground;

    private boolean mRequestingPermissions;

    /**
     * Called when all requirements (like permissions) are satisfied and we are ready
     * to initialize the app.
@@ -141,12 +143,23 @@ public abstract class BaseActivity extends AppCompatActivity implements ServiceC
                ContextCompat.getColor(this, R.color.background_color));
        // Initialize the bottom action bar
        initBottomActionBar();

        // if we are requesting permissions on app launch, we skip binding
        // at onStart() and need to bind after we got permissions and call init()
        // to ensure the UI is properly set up.
        if (mRequestingPermissions) {
            mToken = MusicUtils.bindToService(this, this);
        }
    }

    public boolean isInitialized() {
        return mToken != null;
    }

    public void setRequestingPermissions(final boolean requestingPermissions) {
        mRequestingPermissions = requestingPermissions;
    }

    @Override
    public void onServiceConnected(final ComponentName name, final IBinder service) {
        // Set the playback drawables
@@ -195,8 +208,10 @@ public abstract class BaseActivity extends AppCompatActivity implements ServiceC
    protected void onStart() {
        super.onStart();

        // Bind Eleven's service
        // Bind Eleven's service, if all permissions are granted
        if (!mRequestingPermissions) {
            mToken = MusicUtils.bindToService(this, this);
        }

        final IntentFilter filter = new IntentFilter();
        // Play and pause changes
+2 −0
Original line number Diff line number Diff line
@@ -533,6 +533,8 @@ public class HomeActivity extends SlidingPanelActivity implements
        }

        if (needRequest) {
            setRequestingPermissions(true);

            int count = permissionList.size();
            if (count > 0) {
                String[] permissionArray = new String[count];