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

Commit 758ec43f authored by Mikalacki Sava's avatar Mikalacki Sava
Browse files

Eleven: shake to play next song, only available while music is playing.

Allows user to shake his device to switch to next song.
This feature is available through settings and is invoked
only while music is playing.

Change-Id: Ifb0866565d49443af7f3ac679e80601660506515
parent fd06a775
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -48,6 +48,9 @@
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

    <!-- Accelerometer feature for shake to play -->
    <uses-feature android:name="android.hardware.sensor.accelerometer" />

    <application
        android:name="com.cyanogenmod.eleven.ElevenApplication"
        android:allowBackup="true"
+2 −0
Original line number Diff line number Diff line
@@ -140,6 +140,8 @@
    <string name="settings_show_music_visualization_title">Show music visualization</string>
    <string name="settings_show_lyrics_title">Show song lyrics</string>
    <string name="settings_show_lyrics_summary">For songs that have an srt file</string>
    <string name="settings_shake_to_play">Shake To Play</string>
    <string name="settings_shake_to_play_summary">Shake your device to play next song</string>

    <!-- App widget -->
    <string name="app_widget_small">Music: 4 \u00d7 1</string>
+7 −0
Original line number Diff line number Diff line
@@ -51,6 +51,13 @@
            android:key="show_lyrics"
            android:title="@string/settings_show_lyrics_title"
            android:summary="@string/settings_show_lyrics_summary"/>

        <!-- Shake to switch songs -->
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="shake_to_play"
            android:title="@string/settings_shake_to_play"
            android:summary="@string/settings_shake_to_play_summary"/>
    </PreferenceCategory>
    <!-- Storage catetory -->
    <PreferenceCategory android:title="@string/settings_storage_category" >
+1 −0
Original line number Diff line number Diff line
@@ -48,5 +48,6 @@ interface IElevenService
    int getRepeatMode();
    int getMediaMountedCount();
    int getAudioSessionId();
    void setShakeToPlayEnabled(boolean enabled);
}
+82 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.database.ContentObserver;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Bitmap;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.MediaMetadata;
@@ -66,6 +67,8 @@ import com.cyanogenmod.eleven.provider.SongPlayCount;
import com.cyanogenmod.eleven.service.MusicPlaybackTrack;
import com.cyanogenmod.eleven.utils.BitmapWithColors;
import com.cyanogenmod.eleven.utils.Lists;
import com.cyanogenmod.eleven.utils.PreferenceUtils;
import com.cyanogenmod.eleven.utils.ShakeDetector;
import com.cyanogenmod.eleven.utils.SrtManager;

import java.io.File;
@@ -516,6 +519,25 @@ public class MusicPlaybackService extends Service {
     */
    private MusicPlaybackState mPlaybackStateStore;

    /**
     * Shake detector class used for shake to switch song feature
     */
    private ShakeDetector mShakeDetector;

    private ShakeDetector.Listener mShakeDetectorListener=new ShakeDetector.Listener() {

        @Override
        public void hearShake() {
            /*
             * on shake detect, play next song
             */
            if (D) {
                Log.d(TAG,"Shake detected!!!");
            }
            gotoNext(true);
        }
    };

    /**
     * {@inheritDoc}
     */
@@ -551,6 +573,7 @@ public class MusicPlaybackService extends Service {
            return true;
        }
        stopSelf(mServiceStartId);

        return true;
    }

@@ -738,6 +761,9 @@ public class MusicPlaybackService extends Service {
            mUnmountReceiver = null;
        }

        // deinitialize shake detector
        stopShakeDetector(true);

        // Release the wake lock
        mWakeLock.release();
    }
@@ -2337,6 +2363,7 @@ public class MusicPlaybackService extends Service {
     * Stops playback.
     */
    public void stop() {
        stopShakeDetector(false);
        stop(true);
    }

@@ -2344,6 +2371,7 @@ public class MusicPlaybackService extends Service {
     * Resumes or starts playback.
     */
    public void play() {
        startShakeDetector();
        play(true);
    }

@@ -2402,6 +2430,7 @@ public class MusicPlaybackService extends Service {
            if (mIsSupposedToBePlaying) {
                mPlayer.pause();
                setIsSupposedToBePlaying(false, true);
                stopShakeDetector(false);
            }
        }
    }
@@ -2718,6 +2747,51 @@ public class MusicPlaybackService extends Service {
        notifyChange(PLAYLIST_CHANGED);
    }

    /**
     * Called to set the status of shake to play feature
     */
    public void setShakeToPlayEnabled(boolean enabled) {
        if (D) {
            Log.d(TAG, "ShakeToPlay status: " + enabled);
        }
        if (enabled) {
            if (mShakeDetector == null) {
                mShakeDetector = new ShakeDetector(mShakeDetectorListener);
            }
            // if song is already playing, start listening immediately
            if (isPlaying()) {
                startShakeDetector();
            }
        }
        else {
            stopShakeDetector(true);
        }
    }

    /**
     * Called to start listening to shakes
     */
    private void startShakeDetector() {
        if (mShakeDetector != null) {
            mShakeDetector.start((SensorManager)getSystemService(SENSOR_SERVICE));
        }
    }

    /**
     * Called to stop listening to shakes
     */
    private void stopShakeDetector(final boolean destroyShakeDetector) {
        if (mShakeDetector != null) {
            mShakeDetector.stop();
        }
        if(destroyShakeDetector){
            mShakeDetector = null;
            if (D) {
                Log.d(TAG, "ShakeToPlay destroyed!!!");
            }
        }
    }

    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        /**
         * {@inheritDoc}
@@ -3625,6 +3699,14 @@ public class MusicPlaybackService extends Service {
            return mService.get().getAudioSessionId();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void setShakeToPlayEnabled(boolean enabled) {
            mService.get().setShakeToPlayEnabled(enabled);
        }

    }

}
Loading