summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/dialog/VariableSpeedDialog.java
blob: cf9a2907b12c499461f8ca8511b3537abe58a20a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package de.danoeh.antennapod.dialog;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import java.util.Arrays;
import java.util.List;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.IntentUtils;

public class VariableSpeedDialog {

    private static final String TAG = VariableSpeedDialog.class.getSimpleName();

    private static final Intent playStoreIntent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("market://details?id=com.falconware.prestissimo"));

    private VariableSpeedDialog() {
    }

    public static void showDialog(final Context context) {
        if (org.antennapod.audio.MediaPlayer.isPrestoLibraryInstalled(context)
                || UserPreferences.useSonic()
                || Build.VERSION.SDK_INT >= 23) {
            showSpeedSelectorDialog(context);
        } else {
            showGetPluginDialog(context, true);
        }
    }

    public static void showGetPluginDialog(final Context context) {
        showGetPluginDialog(context, false);
    }

    private static void showGetPluginDialog(final Context context, boolean showSpeedSelector) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
        builder.title(R.string.no_playback_plugin_title);
        builder.content(R.string.no_playback_plugin_or_sonic_msg);
        builder.positiveText(R.string.enable_sonic);
        builder.negativeText(R.string.download_plugin_label);
        builder.neutralText(R.string.close_label);
        builder.onPositive((dialog, which) -> {
            if (Build.VERSION.SDK_INT >= 16) { // just to be safe
                UserPreferences.enableSonic();
                if(showSpeedSelector) {
                    showSpeedSelectorDialog(context);
                }
            }
        });
        builder.onNegative((dialog, which) -> {
            try {
                context.startActivity(playStoreIntent);
            } catch (ActivityNotFoundException e) {
                // this is usually thrown on an emulator if the Android market is not installed
                Log.e(TAG, Log.getStackTraceString(e));
            }
        });
        builder.forceStacking(true);
        MaterialDialog dialog = builder.show();
        if (Build.VERSION.SDK_INT < 16) {
            View pos = dialog.getActionButton(DialogAction.POSITIVE);
            pos.setEnabled(false);
        }
        if(!IntentUtils.isCallable(context.getApplicationContext(), playStoreIntent)) {
            View pos = dialog.getActionButton(DialogAction.NEGATIVE);
            pos.setEnabled(false);
        }
    }

    private static void showSpeedSelectorDialog(final Context context) {
        final String[] speedValues = context.getResources().getStringArray(
                R.array.playback_speed_values);
        // According to Java spec these get initialized to false on creation
        final boolean[] speedChecked = new boolean[speedValues.length];

        // Build the "isChecked" array so that multiChoice dialog is
        // populated correctly
        List<String> selectedSpeedList = Arrays.asList(UserPreferences
                .getPlaybackSpeedArray());
        for (int i = 0; i < speedValues.length; i++) {
            speedChecked[i] = selectedSpeedList.contains(speedValues[i]);
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(R.string.set_playback_speed_label);
        builder.setMultiChoiceItems(R.array.playback_speed_values,
            speedChecked, (dialog, which, isChecked) -> speedChecked[which] = isChecked);
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.setPositiveButton(android.R.string.ok,
            (dialog, which) -> {
                int choiceCount = 0;
                for (boolean checked : speedChecked) {
                    if (checked) {
                        choiceCount++;
                    }
                }
                String[] newSpeedValues = new String[choiceCount];
                int newSpeedIndex = 0;
                for (int i = 0; i < speedChecked.length; i++) {
                    if (speedChecked[i]) {
                        newSpeedValues[newSpeedIndex++] = speedValues[i];
                    }
                }

                UserPreferences.setPlaybackSpeedArray(newSpeedValues);

            });
        builder.create().show();
    }

}